package com.example.nonameaa.controller;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.nonameaa.Common.Result;
import com.example.nonameaa.entity.Message;
import com.example.nonameaa.entity.User;
import com.example.nonameaa.service.MessageService;
import com.example.nonameaa.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author NoName
 * @since 2024-10-12
 */

@RestController
@RequestMapping("/message")
public class MessageController {
    @Resource
    private MessageService messageService;
    /**
     *修改信息
     * @param message
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Message message) {
        messageService.updateById(message);
        return Result.success();
    }

    /**
     * 删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        messageService.removeById(id);
        return Result.success();
    }

    /**
     * 新增信息
     * @param message
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Message message) {
        message.setTime(DateTime.now().toLocalDateTime());
        messageService.save(message);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        messageService.removeByIds(ids);
        return Result.success();
    }

    /**
     * 查询全部信息
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Message> userList = messageService.list(new QueryWrapper<Message>().orderByDesc("id"));
        return Result.success(userList);
    }
}
